package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;

public class IfBlock extends Block{
	private int id;
	private static int next = 1;
	private Point enterControlPoint;
	private Point trueControlPoint;
	private Point falseControlPoint;
	
	public IfBlock(){
		this.id = next++;
	}
	@Override
	public void draw(Graphics2D g) {
		Point point = super.getPoint();		
    	Polygon polygon=new Polygon();	
    	polygon.addPoint( point.x, point.y + 35 );
    	polygon.addPoint( point.x + 35, point.y );
    	polygon.addPoint( point.x + 70, point.y + 35 );
    	polygon.addPoint( point.x + 35, point.y + 70) ;
        g.drawPolygon(polygon);
        g.setColor(new Color(0.5F,0.8F,0.7F,0.5F));
    	g.fillPolygon(polygon);
    	g.setColor(Color.BLACK);
    	g.setFont(new Font("DialogInput", Font.PLAIN, 18));
        g.drawString("X"+id, point.x+27, point.y+38);
        g.setFont(new Font("DialogInput", Font.PLAIN, 14));
        g.drawString("1", point.x+60, point.y+25);
        g.drawString("0", point.x+3, point.y+25);
	}
	@Override
	public void drawControlPoints(Graphics2D g) {
		drawControlPoint(g, enterControlPoint);
		drawControlPoint(g, falseControlPoint);
		drawControlPoint(g, trueControlPoint);
	}
	@Override
	 public void drawZ(Graphics2D g) {
	  Point point = super.getPoint();
	  g.setFont(new Font("DialogInput", Font.PLAIN, 14));
	  g.setColor(Color.GRAY);
	  g.drawString("(z"+this.getZId()+")", point.x+21, point.y+52);
	  g.setColor(Color.BLACK);
	 }
	
	private void drawControlPoint(Graphics2D g, Point cPoint){
		g.setColor(Color.RED);
		g.drawLine(cPoint.x-3, cPoint.y-2, cPoint.x+3, cPoint.y+2);// line \
		g.drawLine(cPoint.x-3, cPoint.y+2, cPoint.x+3, cPoint.y-2);// line /
		Rectangle r = new Rectangle(cPoint.x-3, cPoint.y-2,6,4);
		g.setColor(Color.BLACK);
	}
	@Override
	public int getId() {
		return id;
	}
	@Override
	public void setId(int id) {
		this.id = id;		
	}
	
	@Override
	public boolean contains(Point point) {
		Point selfPoint = super.getPoint();
		Polygon polygon=new Polygon();
		polygon.addPoint( selfPoint.x, selfPoint.y+35);
		polygon.addPoint( selfPoint.x+35, selfPoint.y);
		polygon.addPoint( selfPoint.x+70,selfPoint.y+35);
		polygon.addPoint( selfPoint.x+35, selfPoint.y+70);
		return polygon.contains(point);
	}
	public Point getEnterControlPoint() {
		return enterControlPoint;
	}
	public void setEnterControlPoint(Point enterControlPoint) {
		this.enterControlPoint = enterControlPoint;
	}
	public Point getTrueControlPoint() {
		return trueControlPoint;
	}
	public void setTrueControlPoint(Point trueControlPoint) {
		this.trueControlPoint = trueControlPoint;
	}
	public Point getFalseControlPoint() {
		return falseControlPoint;
	}
	public void setFalseControlPoint(Point falseControlPoint) {
		this.falseControlPoint = falseControlPoint;
	}
	
	@Override
	public void setPoint(Point point) {
		super.setPoint(point);
		calculateControlPoints();
	}
	
	private void calculateControlPoints() {
		Point point = super.getPoint();
		setEnterControlPoint(new Point(point.x + 35, point.y));
		setFalseControlPoint(new Point(point.x, point.y + 35));
		setTrueControlPoint(new Point(point.x + 70, point.y + 35));
	}
	@Override
	public void cleanDraw(Graphics2D g) {
		Point point = super.getPoint();		
    	Polygon polygon=new Polygon();	
    	polygon.addPoint( point.x, point.y + 35 );
    	polygon.addPoint( point.x + 35, point.y );
    	polygon.addPoint( point.x + 70, point.y + 35 );
    	polygon.addPoint( point.x + 35, point.y + 70) ;
    	g.setColor(Color.WHITE);
    	g.drawPolygon(polygon);
    	g.fillPolygon(polygon);
    	g.setFont(new Font("DialogInput", Font.PLAIN, 18));
        g.drawString("X"+id, point.x+27, point.y+38);
        g.setFont(new Font("DialogInput", Font.PLAIN, 14));
        g.drawString("1", point.x+60, point.y+25);
        g.drawString("0", point.x+3, point.y+25);
		
	}
	private boolean isControlPoint(Point point,Point cPoint){
		Rectangle rectangle = new Rectangle(cPoint.x-3, cPoint.y-2,6,4);
		if(rectangle.contains(point)){
			return true;
		}
		return false;
	}
	@Override
	public boolean haveControlPoint(Point point) {
		if(isControlPoint(point, enterControlPoint) || isControlPoint(point, trueControlPoint) || isControlPoint(point, falseControlPoint)){
			return true;
		}
		return false;
	}
	@Override
	public Point getControlPointByClosePoint(Point close) {
		if(isControlPoint(close, enterControlPoint)){
			return enterControlPoint;
		}else if(isControlPoint(close, trueControlPoint)){
			return trueControlPoint;
		}else if (isControlPoint(close, falseControlPoint)){
			return falseControlPoint;
		}
		return null;
	}
	@Override
	public String toString() {
		return "X"+this.getId()+" z="+super.getZId();
	}
	public void setLineType(Point point, Line line) {
		if(isControlPoint(point, trueControlPoint)){
			line.setIfStatu(Line.TRUE);
		}else if(isControlPoint(point, falseControlPoint)){
			line.setIfStatu(Line.FALSE);
		}

	}
	@Override
	public boolean isCausePoint(Point point) {
		if(isControlPoint(point, trueControlPoint) || isControlPoint(point, falseControlPoint))
			return true;
		else 
			return false;
	}
	

}
